package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.fragment.HomeRightFragment1;
import com.example.myapplication.adapter.MennuAdapter;
import com.example.myapplication.R;
import com.example.myapplication.food_message.Menu;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ArrayList<Menu> menuList;
    private MennuAdapter myAdapter;
    private Context mContext;
    private ListView lv;
    private HomeRightFragment1 homeRightFragment1;
    private LinearLayout home_forgment_right1;
    private int sunMoneys = 0;
    private int sumSize = 0;
    private TextView tv_money, tv_sum, textView1;
    private int MenuId = 0;
    private LinearLayout linearLayout;
    private Button listclear;
    boolean actpd = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        homeRightFragment1 = new HomeRightFragment1();
        init();
    }

    private void init() {
        mContext = MainActivity.this;
        listclear = findViewById(R.id.total_money);
        listclear.setOnClickListener(this);
        home_forgment_right1 = findViewById(R.id.home_forgment_right1);
        lv = findViewById(R.id.home_menu);
        tv_sum = findViewById(R.id._home_menu_sum);
        tv_money = findViewById(R.id._home_menu_summoney);
        menuList = new ArrayList<Menu>();
        int foodSize = 9;
        int money = 10;
        int moneys = 10;
        String foodkind = "素菜";
        for (int i = 1; i <= foodSize; i++) {
            if (i == 3) {
                foodkind = "荤菜";
            }
            addMenu(menuList, new Menu(R.mipmap.food, "菜品" + i, money, moneys, 1, MenuId += 1, foodkind, 0));
        }

        //左侧菜品数据展示
        myAdapter = new MennuAdapter(mContext, menuList);
        //    myAdapter.notifyDataSetChanged();
        myAdapter.notifyDataSetChanged();
        lv.setAdapter(myAdapter);


        //计算总价
        totalPrice();
        //写入布局
        ceplaceFragment(homeRightFragment1);
        //传输菜品数据
        homeRightFragment1.setListData(menuList);

    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.total_money:
                TextView mo = findViewById(R.id._home_menu_summoney);
                String s1 = mo.getText().toString();
                Toast.makeText(mContext, "一共" + s1, Toast.LENGTH_SHORT).show();
                //隐藏右侧数据,获取id
                linearLayout = findViewById(R.id.home_forgment_right1);
                if (actpd) {
                    home_forgment_right1.setVisibility(View.INVISIBLE);  //菜单展示节目隐藏
                    listclear.setText("取消订单");
                    actpd = false;
                } else {
                    home_forgment_right1.setVisibility(View.VISIBLE);   //菜单展示节目展示
                    listclear.setText("结算");
                    actpd = true;
                }
                break;
        }
    }

    //切换fragment
    public void ceplaceFragment(Fragment fragment) {
        FragmentManager fragmentManager1 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
        fragmentTransaction1.replace(R.id.home_forgment_right1, fragment);
        fragmentTransaction1.commit();
    }

    //清空菜单
    public void removeAllDate(ArrayList<Menu> date) {
        date.clear();
    }

    //添加菜单
    public void addMenu(ArrayList<Menu> date, Menu menu) {
        boolean bll = true;
        for (int i = 0; i < date.size(); i++) {
            if (menu.getmName().equals(date.get(i).getmName())) {
                int pp = date.get(i).getmSize() + menu.getmSize();
                date.get(i).setmSize(pp);
                bll = false;
            }
        }
        if (bll) {
            date.add(menu);
        }
    }

    //总价计算和展示
    public void totalPrice() {
        int po = 0;
        for (int i = 0; i < menuList.size(); i++) {
            Menu m = menuList.get(i);
            int j, k;
            j = m.getmMoney();
            k = m.getmSize();
            sunMoneys = sunMoneys + (j * k);
        }
        tv_sum.setText(menuList.size() + "");
        //展示总价格
        tv_money.setText(String.valueOf(sunMoneys));

    }

}